Como extraemos los colores?

library(magick)
## Linking to ImageMagick 6.9.10.23
## Enabled features: fontconfig, freetype, fftw, lcms, pango, webp, x11
## Disabled features: cairo, ghostscript, heic, raw, rsvg
## Using 8 threads
my_image <- image_read("./img/ojo 1.png")
image_info(my_image)
##   format width height colorspace matte filesize density
## 1    PNG  1108   1568       sRGB FALSE  2324644   72x72
my_width <- image_info(my_image)$width
my_height <- image_info(my_image)$height

my_raster <- as.raster(my_image)
dim(my_raster)
## [1] 1568 1108
my_width
## [1] 1108
my_height
## [1] 1568
my_vec <- as.vector(my_raster)

Cuantos colores tenemos?

my_vec %>% unique %>% length
## [1] 238160
my_vec %>% length
## [1] 1737344

Reducir numero de colores

my_image_reduced <- image_quantize(
  my_image,
  max=256
)

Vemos como quedó

my_image_reduced

my_image_reduced %>% as.raster() %>% unique() %>% length()
## [1] 256

Intentando mas variantes

my_image_reduced_16 <- image_quantize(
  my_image,
  max=16
)
plot(
  
my_image_reduced_16
)

plot(my_image)

Suficiente?

my_image_reduced_16 %>% as.raster() %>% unique()
##  [1] "#714327ff" "#362c1dff" "#5c7559ff" "#47b099ff" "#909e7aff" "#a5ccabff"
##  [7] "#a35a32ff" "#ca6f37ff" "#d69f69ff" "#e9b3c4ff" "#4cc8b0ff" "#3fd8c4ff"
## [13] "#a0e7ccff" "#dad0a8ff" "#e1eeceff" "#4ee8d3ff"
my_vec <- as.raster(my_image_reduced_16) %>% as.vector()
#replazar uno de los colores al hazar y reconstituir la imagen
my_vec <-  ifelse(my_vec == "#4ee8d3ff","#4ee8d300",my_vec)

Reconstituir

my_matrix <- matrix(ncol=my_width,nrow=my_height,data = my_vec)
as.raster(my_matrix) %>% plot()

No exactamente…

my_matrix <- matrix(ncol=my_width,nrow=my_height,data = my_vec,byrow = TRUE)
as.raster(my_matrix) %>% plot()